package course.dmsSpringBoot.controller;

import course.dmsSpringBoot.entity.Orders;
import course.dmsSpringBoot.entity.ResultMap;
import course.dmsSpringBoot.service.OrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单管理
 * @author zouran
 * createDate：2023/12/17  23:09
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;

    private final ResultMap<Object> resultMap;

    public OrderController(OrderService orderService, ResultMap<Object> resultMap) {
        this.orderService = orderService;
        this.resultMap = resultMap;
    }

    @RequestMapping("/queryAllOrder")
    public ResultMap<List<Orders>> queryAllOrder() {
        return resultMap.success(orderService.list());
    }

    @RequestMapping("/deleteOrderById")
    public ResultMap<Object> deleteOrderById(long id) {
        if (orderService.removeById(id)) {
            return resultMap.successWithMessage("删除成功!");
        }
        return resultMap.successWithMessage("订单id不存在!");

    }
    @PostMapping("/updateOrder")
    public ResultMap<Object> updateOrder(@RequestBody Orders orders) {
        if (orderService.updateById(orders))
            return resultMap.successWithMessage("订单信息更新成功！");
        else return resultMap.codeAndMessage(201, "订单信息更新失败！Id不存在");
    }

    @PostMapping("/addOrUpdateOrder")
    public ResultMap<Object> addOrder(@RequestBody Orders orders) {
        if (orderService.saveOrUpdate(orders))
            return resultMap.successWithMessage("订单添加成功！");
        else return resultMap.codeAndMessage(201, "订单添加失败！");
    }
}


